package com.platform.core.web.controller;

import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.platform.comm.util.Base64ImageUtils;
import com.platform.core.service.SysConfService;
import com.platform.dto.ConfQo;
import com.querydsl.core.BooleanBuilder;
import com.platform.comm.web.results.DataJsonResult;
import com.platform.comm.web.results.JsonResult;
import com.platform.comm.web.results.PageListJsonResult;
import org.apache.logging.log4j.util.Strings;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Description: 参数配置管理
 *
 * @author libin
 * Created on 2019/4/23
 **/
@RestController
@RequestMapping("management/conf")
public class SysConfController extends BaseController {

    private final SysConfService sysConfService;

    public SysConfController(SysConfService sysConfService) {
        this.sysConfService = sysConfService;
    }

    @PostMapping(name = "新增参数配置", path = "create")
    public JsonResult create(@RequestBody ConfQo confQo) {
        return createOrModifyWithResult(sysConfService::create, confQo);
    }

    @PostMapping(name = "图片上传",path = "/uploadPic")
    public JsonResult uploadPic(@RequestParam MultipartFile uploadPic){
        String base64Str = "";
        try {
            base64Str = Base64ImageUtils.encodeImageToBase64(uploadPic.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return DataJsonResult.succeed(base64Str);
    }

    @PostMapping(name = "更新参数配置", path = "modify")
    public JsonResult modify(@RequestBody ConfQo confQo) {
        return createOrModifyWithResult(sysConfService::modify, confQo);
    }

    @PostMapping(name = "删除参数配置", path = "remove")
    public JsonResult remove(@RequestBody Map<String, String> map) {
        String id = Objects.toString(map.get("id"), Strings.EMPTY);
        return removeOrOtherByIds(sysConfService::remove, id);
    }

    @PostMapping(name = "分页查询", path = "search")
    public PageListJsonResult<ConfQo> search(@RequestBody Map<String, String> map) {
        MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
        if (CollUtil.isNotEmpty(map)) {
            map.forEach(parameters::add);
        }
        return queryByPaging(sysConfService::queryConfs, new BooleanBuilder(), getPageable(map), parameters);
    }

    @PostMapping(name = "查询信息列表(不分页)", path = "searchNoPage")
    public DataJsonResult<List<ConfQo>> searchNoPage(@RequestBody Map<String, String> map) {
        MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
        if (CollUtil.isNotEmpty(map)) {
            map.forEach(parameters::add);
        }
        return query(sysConfService::queryConfs, new BooleanBuilder(), parameters);
    }

    @PostMapping(name = "检查参数配置编码", path = "checkCodeExists")
    public DataJsonResult<Long> checkCodeExists(@RequestBody Map<String, String> map) {
        String id = Objects.toString(map.get("id"), Strings.EMPTY);
        String confCode = Objects.toString(map.get("confCode"), Strings.EMPTY);
        String typeId = Objects.toString(map.get("typeId"), Strings.EMPTY);
        return query(sysConfService::checkCodeExists, id, confCode,typeId);
    }

    @PostMapping(name = "检查参数配置名称", path = "checkNameExists")
    public DataJsonResult<Long> checkNameExists(@RequestBody Map<String, String> map) {
        String id = Objects.toString(map.get("id"), Strings.EMPTY);
        String confName = Objects.toString(map.get("confName"), Strings.EMPTY);
        String typeId = Objects.toString(map.get("typeId"), Strings.EMPTY);
        return query(sysConfService::checkNameExists, id, confName,typeId);
    }

    /**
     * 通过编码获取详情信息-配置信息为多选/单选
     * @param map {"code":"abc"}
     * @return 配置详情[{"key":"","value":""},{"key":"","value":""}]
     */
    @PostMapping(name = "获取配置信息", path = "getConfInfo")
    public DataJsonResult<List<JsonNode>> getConfInfo(@RequestBody Map<String,String> map){
        String code = Objects.toString(map.get("code"),Strings.EMPTY);
        return query(sysConfService::getConfInfo,code);
    }

    /**
     * 通过编码获取详情信息-配置信息为多选/单选
     * @param code 编码
     * @return 配置详情[{"key":"","value":""},{"key":"","value":""}]
     */
    @PostMapping(name = "获取配置信息", path = "getConfList")
    public List<Map<String, String>> getConfList(String code){
        return sysConfService.getConfList(code);
    }

    /**
     * 通过编码获取详情信息-配置信息为单选
     * @param map {"code":"abc"}
     * @return value
     */
    @PostMapping(name = "获取配置信息", path = "getSingleConfInfo")
    public DataJsonResult<String> getSingleConfInfo(@RequestBody Map<String,String> map){
        String code = Objects.toString(map.get("code"),Strings.EMPTY);
        return query(sysConfService::getSingleConfInfo,code);
    }
    @PostMapping(name = "获取配置信息", path = "getSingleConfInfoRpc")
    public Map<String,Object> getSingleConfInfoRpc(@RequestParam("groupCode") String groupCode,
                                                   @RequestParam("itemCode") String itemCode){
        return sysConfService.getSingleConfInfo(groupCode,itemCode);
    }

    /**
     * 获取字典项信息
     * @param map {"groupCode":"abc","itemCode":"123"}
     * @return {"123":"value"}
     */
    @PostMapping(name = "获取字典项配置信息", path = "getConfDictInfo")
    public DataJsonResult<Map<String,Object>> getConfDictInfo(@RequestBody Map<String,String> map){
        String groupCode = Objects.toString(map.get("groupCode"),Strings.EMPTY);
        String itemCode = Objects.toString(map.get("itemCode"),Strings.EMPTY);
        return query(sysConfService::getSingleConfInfo,groupCode,itemCode);
    }

    @PostMapping(name = "获取字典项配置信息", path = "getSingleValueRpc")
    public DataJsonResult<Object> getSingleValueRpc(@RequestParam("groupCode") String groupCode,
                                                 @RequestParam("itemCode") String itemCode){
        return query(sysConfService::getSingleValue,groupCode,itemCode);
    }

    @PostMapping(name = "获取字典项配置信息", path = "getSingleValue")
    public DataJsonResult<Object> getSingleValue(@RequestBody Map<String,String> map){
        String groupCode = Objects.toString(map.get("groupCode"),Strings.EMPTY);
        String itemCode = Objects.toString(map.get("itemCode"),Strings.EMPTY);
        return query(sysConfService::getSingleValue,groupCode,itemCode);
    }

    /**
     * 获取配置基础信息
     * @param map {"code":"abc"}
     * @return ConfQo
     */
    @PostMapping(name = "获取参数配置基础信息", path = "getConfBaseInfo")
    public DataJsonResult<ConfQo> getConfBaseInfo(@RequestBody Map<String,String> map){
        String code = Objects.toString(map.get("code"),Strings.EMPTY);
        return DataJsonResult.succeed(sysConfService.queryByCode(code));
    }

    /**
     * 根据编码获取配置信息
     *
     * @param code 参数编码
     * @return 配置
     */
    @PostMapping(name = "获取参数配置基础信息", path = "getConfBaseInfoRpc")
    public ConfQo queryByCode(@RequestParam("code") String code) {
        return sysConfService.queryByCode(code);
    }

    /**
     * 获取配置信息
     *
     * @param code 参数编码
     * @return 配置信息列表
     */
    @PostMapping(name = "获取参数配置基础信息", path = "getConfInfo/{code}")
    public List<JsonNode> getConfInfo(@PathVariable String code) {
        return sysConfService.getConfInfo(code);
    }

    /**
     * 根据routeCode查询字典信息
     *
     * @param routeCode 路由编码
     * @return
     */
    @PostMapping(path = "findConfByRouteCode")
    public ConfQo findConfByRouteCode(String routeCode) {
        return sysConfService.findConfByRouteCode(routeCode);
    }

    /**
     * 通过编码获取字典项配置信息
     * @param map {"code":"abc"}
     * @return 配置详情[{"key":"","value":""},{"key":"","value":""}]
     */
    @PostMapping(name = "获取字典项信息", path = "getDictConfInfo")
    public DataJsonResult<List<Map<String,Object>>> getDictConfInfo(@RequestBody Map<String,String> map){
        String code = Objects.toString(map.get("code"),Strings.EMPTY);
        return query(sysConfService::getDictConfInfo,code);
    }

    /**
     * 通过编码获取字典项配置信息
     * @param map {"code":"abc"}
     * @return 配置详情[{"key":"","value":""},{"key":"","value":""}]
     */
    @PostMapping(name = "获取字典项信息", path = "getDictConfInfoApi")
    public List<Map<String,Object>> getDictConfInfoApi(@RequestBody Map<String,String> map){
        String code = Objects.toString(map.get("code"),Strings.EMPTY);
        return sysConfService.getDictConfInfo(code);
    }

    /**
     * 通过编码集合获取字典项配置信息
     */
    @PostMapping(name = "获取字典项信息", path = "searchDictConfInfos")
    public DataJsonResult<Map<String,List<Map<String,Object>>>> searchDictConfInfos(@RequestBody List<String> list){
        return query(sysConfService::searchDictConfInfos,list);
    }

    /**
     * 通过编码集合获取字典项配置信息
     */
    @PostMapping(name = "获取字典项信息", path = "searchDictConfInfosApi")
    public Map<String,List<Map<String,Object>>> searchDictConfInfosApi(@RequestBody List<String> list){
        return sysConfService.searchDictConfInfos(list);
    }

    @PostMapping(name = "分页查询字典组信息", path = "searchDictGroup")
    public PageListJsonResult<ConfQo> searchDictGroup(@RequestBody Map<String, String> map) {
        MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
        if (CollUtil.isNotEmpty(map)) {
            map.forEach(parameters::add);
        }
        return queryByPaging(sysConfService::searchDictGroup, new BooleanBuilder(), getPageable(map), parameters);
    }

    @PostMapping(path = "getSingleConfInfo/{groupCode}/{itemCode}")
    public Map<String, Object> getSingleConfInfo(@PathVariable String groupCode, @PathVariable String itemCode) {
        return sysConfService.getSingleConfInfo(groupCode, itemCode);
    }

    @PostMapping(path = "management/conf/getSingleValue/{groupCode}/{itemCode}")
    public Object getSingleValue(@PathVariable String groupCode, @PathVariable String itemCode) {
        return sysConfService.getSingleValue(groupCode, itemCode);
    }

    @PostMapping(name = "分页查询系统级别字典", path = "searchDict")
    public PageListJsonResult<ConfQo> searchSysDict(@RequestBody(required = false) Map<String, String> map) {
        MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
        if (CollUtil.isNotEmpty(map)) {
            map.forEach(parameters::add);
        }
        parameters.set("routePrefix","clz-conf-dictGroup-system-");
        parameters.set("valueMode","3");
        return queryByPaging(sysConfService::queryConfs, new BooleanBuilder(), getPageable(map), parameters);
    }

    @PostMapping(name = "创建平台字典规范", path = "createDict")
    public JsonResult createSysDict(@RequestBody ConfQo confQo) {
        return createOrModifyWithResult(sysConfService::createSysDict, confQo);
    }

}
